package service

import "github.com/gin-gonic/gin"
import "go_part/utils"
import "fmt"
import "time"
import "math/rand"
import "os"
import "strings"
import "io"

func Upload(c *gin.Context){
	w := c.Writer
	request := c.Request
	srcFile, head, err := request.FormFile("file")
	if err != nil {
			utils.RespFail(w,err.Error())
	}
	suffix := ".png"
	ofilName := head.Filename
	tem := strings.Split(ofilName,".")
	if len(tem) >1 {
		suffix = "." + tem[len(tem)-1]
	}
	fileName := fmt.Sprintf("%d%04%s",time.Now().Unix(), rand.Int31(),suffix)
	dstFile,err := os.Create("./upload/"+fileName)
	if err != nil {
		utils.RespFail(w,err.Error())
	}
	_,err = io.Copy(dstFile,srcFile)
	if err != nil {
		utils.RespFail(w,err.Error())
	}
	//url := "./upload/"+fileName
	utils.RespOK(w,"发送图片成功")

}